package cn.edu.bzu.ie.sf.mealsmisweb.controller;

import cn.edu.bzu.ie.sf.mealsmisweb.common.MyUtils;
import cn.edu.bzu.ie.sf.mealsmisweb.entity.Fireman;
import cn.edu.bzu.ie.sf.mealsmisweb.service.FiremanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version v1.0.0
 * @Package : cn.edu.bzu.ie.sf.mealsmisweb.controller
 * @Description : 消防员控制器类
 * @Create on : 2021/8/12 19:17
 * @author: Feng Song 宋锋
 **/
@RestController
@RequestMapping(value="/fireman")
public class FiremanController {
    @Autowired
    private FiremanService firemanService;   // 消防员业务实例

    /**
     * 取得某个部门下的所有的消防员信息
     * @param depid 部门信息
     * @return List<Fireman> 消防员集合
     */
    @RequestMapping(value="/getFiremanListByDepid")
    public List<Fireman> getFiremanListByDepid(int depid){
        return firemanService.getFiremanListByDepid(depid);
    }

    /**
     * 获取某个部门下的所有消防员信息、今天生日的消防员、明天生日的消防员信息
     * @param depid 当前的中队部门信息
     * @return Map<String, Ojbect> firemanList, birthToday, birthTomorrow
     */
    @RequestMapping(value="/getFiremanAndBirthTodayAndTomorrowList")
    public Map<String, Object> getFiremanAndBirthTodayAndTomorrowList(int depid){
        Map<String, Object> map = new HashMap<>();
        List<Fireman> firemanList = firemanService.getFiremanListByDepid(depid);
        map.put("firemanList", firemanList);
        String birthToday = "";     // 今天过生日人员
        String birthTomorrow = "";  // 明天过生日成员
        for(Fireman item : firemanList){
            // 今天过生日
            if(item.getBirthstr().equals(MyUtils.getDateStrFromNow().substring(5))){
                if(MyUtils.isEmpty(birthToday)){
                    birthToday = item.getFm_name();
                }else{
                    birthToday += "，" + item.getFm_name();
                }
            }
            // 明天过生日
            if(item.getBirthstr().equals(MyUtils.getDateStrFromTomorrow().substring(5))){
                if(MyUtils.isEmpty(birthTomorrow)){
                    birthTomorrow = item.getFm_name();
                } else {
                    birthTomorrow += "，" + item.getFm_name();
                }
            }
        }
        map.put("birthToday", birthToday);
        map.put("birthTomorrow", birthTomorrow);
        return map;
    }

    /**
     * 取得某个部门下的所有明天过生日的消防员信息
     * @param depid 部门信息
     * @return List<Fireman> 消防员集合
     */
    @RequestMapping(value="/getFiremanListByDepidAndBirthTomorrow")
    public List<Fireman> getFiremanListByDepidAndBirthTomorrow(int depid, String dtTomorrow){
        return firemanService.getFiremanListByDepidAndBirthTomorrow(depid, dtTomorrow);
    }


    /**
     * 添加消防员信息的方法
     * @param item 要添加的消防员信息
     * @return int 影响的行数
     */
    @RequestMapping(value="/addFireman")
    public int addFireman(Fireman item){
        return firemanService.addFireman(item);
    }

    /**
     * 修改消防员信息的方法
     * @param item 要修改的消防员信息
     * @return int 影响的行数
     */
    @RequestMapping(value = "/editFireman")
    public int editFireman(Fireman item){
        return firemanService.editFireman(item);
    }

    /**
     * 删除消防员信息的方法
     * @param id 要删除的消防员信息
     * @return int 影响的行数
     */
    @RequestMapping(value = "/delFireman")
    public int delFireman(int id){
        return firemanService.delFireman(id);
    }
}
